package com.ruoyi.supplier.controller;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.supplier.domain.TbProductPack;
import com.ruoyi.supplier.service.ITbProductPackService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 包装Controller
 * 
 * @author ruoyi
 * @date 2021-06-18
 */
@RestController
@RequestMapping("/supplier/productPack")
public class TbProductPackController extends BaseController
{
    @Autowired
    private ITbProductPackService tbProductPackService;

    /**
     * 查询包装列表
     */
    @PreAuthorize("@ss.hasPermi('supplier:productPack:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbProductPack tbProductPack)
    {
        startPage();
        List<TbProductPack> list = tbProductPackService.selectTbProductPackList(tbProductPack);
        return getDataTable(list);
    }

    /**
     * 导出包装列表
     */
    @PreAuthorize("@ss.hasPermi('supplier:productPack:export')")
    @Log(title = "包装", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TbProductPack tbProductPack)
    {
        List<TbProductPack> list = tbProductPackService.selectTbProductPackList(tbProductPack);
        ExcelUtil<TbProductPack> util = new ExcelUtil<TbProductPack>(TbProductPack.class);
        return util.exportExcel(list, "productPack");
    }

    /**
     * 获取包装详细信息
     */
    @PreAuthorize("@ss.hasPermi('supplier:productPack:query')")
    @GetMapping(value = "/{packId}")
    public AjaxResult getInfo(@PathVariable("packId") Long packId)
    {
        return AjaxResult.success(tbProductPackService.selectTbProductPackById(packId));
    }

    /**
     * 获取包装详细信息
     */
    @PreAuthorize("@ss.hasPermi('supplier:productPack:query')")
    @GetMapping(value = "/productId/{productId}")
    public AjaxResult getProductId(@PathVariable("productId") Long productId)
    {
        List<TbProductPack> list = tbProductPackService.selectTbProductPackByProductId(productId);
        AtomicInteger key = new AtomicInteger(100000);
        list.forEach(tp -> {
            key.getAndIncrement();
            tp.setAutoId(key.get());
        });
        return AjaxResult.success(list);
    }

    /**
     * 新增包装
     */
    @PreAuthorize("@ss.hasPermi('supplier:productPack:add')")
    @Log(title = "包装", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbProductPack tbProductPack)
    {
        return toAjax(tbProductPackService.insertTbProductPack(tbProductPack));
    }

    /**
     * 修改包装
     */
    @PreAuthorize("@ss.hasPermi('supplier:productPack:edit')")
    @Log(title = "包装", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbProductPack tbProductPack)
    {
        return toAjax(tbProductPackService.updateTbProductPack(tbProductPack));
    }

    /**
     * 删除包装
     */
    @PreAuthorize("@ss.hasPermi('supplier:productPack:remove')")
    @Log(title = "包装", businessType = BusinessType.DELETE)
	@DeleteMapping("/{packIds}")
    public AjaxResult remove(@PathVariable Long[] packIds)
    {
        return toAjax(tbProductPackService.deleteTbProductPackByIds(packIds));
    }
}
